/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
/**
 * @description CuxControllerSupport.java
 * @author dongbin
 * @version 
 * @copyright
 */

package com.erp.cux.workflow.controller;

import com.erp.hr.dao.model.HrDepartment;
import com.erp.hr.dao.model.HrStaffInfoRO;
import org.apache.commons.lang.StringUtils;
import org.redragon.commons.i18n.i18n;
import org.redragon.framework.springmvc.ControllerSupport;
import org.redragon.framework.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import com.erp.cux.workflow.dao.model.CuxWorkflowRecord;
import com.erp.cux.workflow.service.CuxWorkflowRecordService;
import com.erp.hr.service.HrCommonService;

/**
 * @description
 * @date 2021年3月6日
 * @author dongbin
 * 
 */
public abstract class CuxControllerSupport extends ControllerSupport {

    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private CuxWorkflowRecordService cuxWorkflowRecordService;

    public HrStaffInfoRO getLoginStaffInfo(){
        HrStaffInfoRO staffInfo = this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal());
        HrDepartment hrDepartmentget=this.hrCommonService.getHrDepartmentParentCompany(staffInfo.getDepartmentCode());
        staffInfo.setCompanyName(hrDepartmentget.getDepartmentName());
        staffInfo.setCompanyCode(hrDepartmentget.getDepartmentCode());
        return staffInfo;
    }
    
    /**
     * 
     * @description 设置编辑页面的工作流model参数
     * @date 2021年3月6日
     * @author dongbin
     * @param model
     * @param billType
     * @param billHeadCode
     * @return void
     *
     */
    public void setWorkflowModel(Model model, String billType, String billHeadCode) {
        //设置工作流生效标识
        String workflowFlag = i18n.getSingleValue("workflow", billType+"_WORKFLOW");
        if(StringUtils.isBlank(workflowFlag)) {
            workflowFlag = "N";
        }
        model.addAttribute("workflowFlag", workflowFlag);
        
        //设置工作流审批权限标识
        model.addAttribute("workflowControlFlag", "N");
        if(StringUtils.isNotBlank(billHeadCode)) {
            CuxWorkflowRecord cuxWorkflowRecord = this.cuxWorkflowRecordService.getCurrentRecordByBill(billType, billHeadCode);
            if(cuxWorkflowRecord!=null&&this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal()).getStaffCode().equals(cuxWorkflowRecord.getApprovePerson())) {
                model.addAttribute("workflowControlFlag", "Y");
            }
        }
    }
    
    
    
    /**
     * 
     * @description 设置列表页面的工作流model参数
     * @date 2021年3月6日
     * @author dongbin
     * @param model
     * @param billType
     * @return void
     *
     */
    public void setWorkflowModel(Model model, String billType) {
        //设置工作流生效标识
        String workflowFlag = i18n.getSingleValue("workflow", billType+"_WORKFLOW");
        if(StringUtils.isBlank(workflowFlag)) {
            workflowFlag = "N";
        }
        model.addAttribute("workflowFlag", workflowFlag);
    }

}
